<?php
/**
 * Created by PhpStorm.
 * User: 户学
 * Date: 2018/6/22
 * Time: 15:27
 */

class SecooCrypt
{
    private $iv = "0102030405060708";//密钥偏移量IV

    //获取请求数据密文
    public function requestEncrypt($aesKey,$md5Key,$midPlatform,$version,$json){
//        echo "--加密前->>" . $json;
        $md5Str = $this -> md5($json,$md5Key);
        $aesStr = $this -> encrypt($json,$aesKey);
        $reqJson = "{\"sign\":\"" . $md5Str . "\",\"data\":\"" . $aesStr . "\",\"midPlatform\":\"" . $midPlatform . "\",\"version\":\"" . $version . "\"}";
//        echo "--加密hou->>" . $json;
        return $reqJson;
    }

    public function responseDecrypt($aesKey,$md5Key,$resultJson){
        $arr = json_decode($resultJson);
        $data = $arr ->data;
//        echo "--解密前->>" . $data;
        $aesStr = trim($this -> decrypt($data,$aesKey));
        $aesStr = substr($aesStr,0,strripos($aesStr,"}")+1);
//        echo "--解密后->>" .$aesStr."\n";
        //验签
        $sign = $arr->sign;
        $md5Str = $this -> md5($aesStr,$md5Key);
        if($sign == $md5Str){
//            echo "验签通过，解密成功";
            return $aesStr;
        }else{
            echo "验签失败";
        }
    }
    //AES加密
    //$encryptStr  明文
    //$encryptKey  aes_key
//    private function encrypt($encryptStr,$encryptKey) {
//        $localIV = $this->iv;
//        $encryptKey = $this->get128Key($encryptKey);
//        //Open module
//        $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, $localIV);
//
//
//        mcrypt_generic_init($module, $encryptKey, $localIV);
//
//        //Padding
//        $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
//        $pad = $block - (strlen($encryptStr) % $block);
//        $encryptStr .= str_repeat(chr($pad), $pad);
//
//        //encrypt
//        $encrypted = mcrypt_generic($module, $encryptStr);
//
//        //Close
//        mcrypt_generic_deinit($module);
//        mcrypt_module_close($module);
//
//        return strtoupper(bin2hex($encrypted));
//
//    }

    //AES解密
    //$decryptStr  密文
    //$encryptKey  aes_key
//    private function decrypt($decryptStr,$encryptKey) {
//        $localIV = $this->iv;
//        $encryptKey = $this->get128Key($encryptKey);
//        //Open module
//        $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, $localIV);
//
//        mcrypt_generic_init($module, $encryptKey, $localIV);
//
//        $decryptedData = hex2bin($decryptStr);
//        $decryptedData = mdecrypt_generic($module, $decryptedData);
//
//        return $decryptedData;
//    }
    //新版本加密与解密开始
    public  function encrypt($input, $key)
    {
        $data = openssl_encrypt($input, 'AES-128-ECB', $key, OPENSSL_RAW_DATA);
        $data = base64_encode($data);
        return $data;
    }

    /**
     * [decrypt aes解密]
     * @param [type]     $sStr [要解密的数据]
     * @param [type]     $sKey [加密key]
     * @return [type]       [解密后的数据]
     */
    public function decrypt($sStr, $sKey)
    {
        $decrypted = openssl_decrypt(base64_decode($sStr), 'AES-128-ECB', $sKey, OPENSSL_RAW_DATA);
        return $decrypted;
    }
    //新版本加密解密结束

    //MD5
    //$encryptStr 明文
    //$md5_key  MD5_KEY
    private function md5($encryptStr,$md5_key){

        return md5($encryptStr . $md5_key);
    }

    private function get128Key($encryptKey){
        if(strlen($encryptKey)>16){
            return substr($encryptKey,0,16);
        }else{
            return str_pad($encryptKey,16,0,STR_PAD_RIGHT);
        }
    }

}